package com.xiaomi.baidumap.github;



import com.xiaomi.baidumap.github.view.RepoListView;
import com.xiaomi.baidumap.model.Repo;
import com.xiaomi.baidumap.model.RepoResult;
import com.xiaomi.baidumap.network.GithubClient;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 业务类，主要完成数据加载，通知视图进行改变
 * 作者：yuanchao on 2016/8/24 0024 16:29
 * 邮箱：yuanchao@feicuiedu.com
 */
public class RepoListPresenter {

    private RepoListView repoListView;
    private Language language;
    private int nextPage = 1;

    private Call<RepoResult> repoCall;

    public RepoListPresenter(RepoListView repoListView,Language language) {

        this.repoListView = repoListView;
        this.language = language;
    }

    public void refresh(){
        //显示刷新
        repoListView.showContentView();
        //每次刷新都是从第一页开始的
        nextPage = 1;
        repoCall = GithubClient.getInstance().searchRepos("language:" + language.getPath(), nextPage);
        //进行异步处理
        repoCall.enqueue(repoCallBack);
    //    new Refresh().execute();
    }

    //加载更多的方法
    public void loadMore(){
        repoListView.showLoadingView();
        repoCall = GithubClient.getInstance().searchRepos("language:" + language.getPath(), nextPage);
        //进行异步处理
        repoCall.enqueue(loadMoreCallBack);
        //  new LoadMore().execute();
    }
    //异步获取仓库
    private Callback<RepoResult> repoCallBack = new Callback<RepoResult>(){

        @Override
        public void onResponse(Call<RepoResult> call, Response<RepoResult> response) {

            //判断结果是不是为空//停止刷新
            repoListView.stopRefresh();
            RepoResult repoResult = response.body();

            if (repoResult == null){
                repoListView.showErrorView("结果为空");
                return;
            }

            if (repoResult.getTotalCount() <= 0){
                //
                repoListView.refreshData(null);
                repoListView.showEmptyView();
                return;
            }

            List<Repo> repoList = repoResult.getRepoList();
            repoListView.refreshData(repoList);
            //下拉刷新第一页数据完成，下一次进行加载从第二页开始
            nextPage = 2;
        }

        @Override
        public void onFailure(Call<RepoResult> call, Throwable t) {
            //刷新失败的处理
            repoListView.stopRefresh();
            repoListView.showMessage(t.getMessage());
        }
    };

    private Callback<RepoResult> loadMoreCallBack = new Callback<RepoResult>() {
        @Override
        public void onResponse(Call<RepoResult> call, Response<RepoResult> response) {
            RepoResult repoResult = response.body();
            if (repoResult == null){
                repoListView.showLoadError("结果为空");
                return;
            }

            //不为空
            List<Repo> repos = repoResult.getRepoList();
            repoListView.addLoadData(repos);
            nextPage++;
        }

        @Override
        public void onFailure(Call<RepoResult> call, Throwable t) {
            //首先关闭加载的
            repoListView.hideLoadView();
            //然后打印出错误的信息
            repoListView.showMessage(t.getMessage());
        }
    };
}
